package com.study.demo.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @author: elvin
 * @time: 2019-05-06 15:22
 * @desc:
 **/
@Controller
@RequestMapping("/arg")
public class ArgController {

    @GetMapping("/showOrders")
    public String showOrders(Map<String, Object> map, HttpServletRequest request) throws IOException {
        System.out.println("request: local - " + request.getParameter("local"));
        for (String key : map.keySet()) {
            System.out.println( key + " - " + map.get(key));
            request.setAttribute(key, map.get(key));
        }
        return "orders";
    }

    @GetMapping("/submit")
    public String submit(RedirectAttributes attr) throws IOException{
        ((FlashMap)((ServletRequestAttributes)(RequestContextHolder.getRequestAttributes()))
                .getRequest()
                .getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE))
                .put("name", "xiaoming");
        attr.addFlashAttribute("ordersId", "xxx");
        //这个参数会拼接到 url 上去
        attr.addAttribute("local", "zh-cn");
        return "redirect:showOrders";
    }
}
